<?php

class Db{ 
        const DEF_SERVER = 'localhost';
        const DEF_USERNAME = 'root';
        const DEF_PASS = 'foo';
        const DEF_DBNAME = 'biller';
        
        private $db = NULL;
        
        private function cdr_read($filter='', $join_tbl='', $join_cond='', $join_type='INNER'){
            $sql_q = "SELECT cdrs.cdr_id, cdrs.ocg, cdrs.ocd, cdrs.clock_org, cdrs.clock_con,
                cdrs.duration, cdrs.money FROM cdrs";
            $ret = NULL;
            if ($join_tbl != ''){
                $sql_q .= " ".$join_type." JOIN ".$join_tbl." ON ".$join_cond;
            }
            if ($filter != '')
                $sql_q .= " WHERE ".$filter;
            
            $rset = $this->db->query($sql_q);
            //echo $rset->num_rows;
            //var_dump($rset);
            $ret = array();
            for($i=0; $i<$rset->num_rows; $i++)
                $ret[$i] = $rset->fetch_assoc();
            
            return $ret;
        }
           
        public function __construct($db_server=self::DEF_SERVER, $db_user=self::DEF_USERNAME, $db_pass=self::DEF_PASS, $db_dbname=self::DEF_DBNAME) {
            $this->db = new mysqli($db_server, $db_user, $db_pass, $db_dbname);
            
            if ($this->db->connect_error)
                echo "Error {$this->db->connect_errno}: {$this->db->connect_error}";
        }
        
        public function __destruct() {
            $this->db->close();
        }
        
        public function get_dep_cdrs($dep_id){
            $join_tbl = "users";
            $join_cond = "cdrs.ocg=users.dn OR cdrs.ocd=users.dn";
            $filter = 'users.dept_id='.$dep_id;
            return $this->cdr_read($filter, $join_tbl, $join_cond);
        }
        
        public function get_ext_cdrs($ext){
            $filter = "ocg=$ext OR ocd=$ext";
            return $this->cdr_read($filter);
        }
        
        public function get_emp_cdrs($emp_id){
            $join_tbl = "users";
            $join_cond = "cdrs.ocg=users.dn OR cdrs.ocd=users.dn";
            $filter = 'users.uid='.$emp_id;
            return $this->cdr_read($filter, $join_tbl, $join_cond);
        }
        
        public function get_all_cdrs(){
            return $this->cdr_read();
        }
        
        public function get_all_extensions(){
            $sql_q = "SELECT DISTINCT dn FROM users";
            $rset = $this->db->query($sql_q);
            $ret = array();

            while($row = $rset->fetch_array(MYSQLI_NUM))
                array_push ($ret, $row[0]);
            return $ret;
        }
        
        public function get_all_deps(){
            $this->db->query("SET NAMES utf8");
            $sql_q = "SELECT dept_id AS id, dept_name AS name FROM departments";
            $rset = $this->db->query($sql_q);
            $ret = array();
            
            while($row = $rset->fetch_assoc()){
                array_push ($ret, $row);
            }
            return $ret;
        }
        
        public function get_all_emps(){
            $this->db->query("SET NAMES utf8");
            $sql_q = "SELECT uid AS id, displayname AS name FROM users";
            $rset = $this->db->query($sql_q);
            $ret = array();
            
            while($row = $rset->fetch_assoc()){
                array_push ($ret, $row);
            }
            return $ret;
        }
        
        public function get_emp_ext($emp_id){
            $sql_q = "SELECT dn from users WHERE uid=$emp_id";
            $rset = $this->db->query($sql_q);
            $ret = $rset->fetch_all();
     
            return $ret;
        }
        
        public function get_dep_ext($dep_id){
            $sql_q = "SELECT users.dn from users INNER JOIN departments ON users.dept_id=departments.dept_id WHERE departments.dept_id=$dep_id";
            $rset = $this->db->query($sql_q);
            $ret = $rset->fetch_all();
     
            return $ret;
        }
    };

?>
